﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OpenTKLightingTool.Data;

namespace OpenTKLightingTool.Controls
{
    /// <summary>
    /// Interaction logic for LightsList.xaml
    /// </summary>
    public partial class LightsList : UserControl
    {
        private LightDictionary mData;
        private int mPreviousIndex = -1;

        public LightsList()
        {
            InitializeComponent();

            uxLights.SelectionUnit = DataGridSelectionUnit.FullRow;
            uxLights.SelectionMode = DataGridSelectionMode.Single;
            uxLights.SelectionChanged += uxLights_SelectionChanged;
            VirtualizingStackPanel.SetIsVirtualizing(uxLights, false);
        }

        private void Checkbox_Checked(object sender, RoutedEventArgs e)
        {
            mData[((int)(sender as CheckBox).Tag)].IsEnabled = true;
            e.Handled = true;
        }

        private void Checkbox_Unchecked(object sender, RoutedEventArgs e)
        {
            mData[((int)(sender as CheckBox).Tag)].IsEnabled = false;
            e.Handled = true;
        }

        private void uxLights_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (mPreviousIndex >= 0)
            {
                bool visible = mData[mPreviousIndex].IsEnabled;
                mData[mPreviousIndex] = uxProperties.GetLight(visible);
            }
            if (uxLights.SelectedIndex >= 0)
            {
                uxProperties.SetLight(mData[uxLights.SelectedIndex]);
            }
             
            mPreviousIndex = uxLights.SelectedIndex;
        }

        internal void SetData(LightDictionary data)
        {
            mData = data;
            uxProperties.SetLight(data[0]);
            uxLights.ItemsSource = null;
            uxLights.ItemsSource = data.EnumLights();
            uxLights.SelectedIndex = 0;
        }

        internal void Update()
        {
            if (mPreviousIndex >= 0)
            {
                bool visible = mData[mPreviousIndex].IsEnabled;
                mData[mPreviousIndex] = uxProperties.GetLight(visible);
            }
        }
    }
}
